package cn.sumpu.app.guoguojie.dao.impl;

import java.util.List;
import java.util.Map;

import org.mybatis.spring.support.SqlSessionDaoSupport;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.sumpu.app.guoguojie.dao.BlogContentDao;
import cn.sumpu.app.guoguojie.domain.BlogContent;
import cn.sumpu.app.guoguojie.exception.AppBusinessException;

public class BlogContentDaoImpl extends SqlSessionDaoSupport implements BlogContentDao {
	private static Logger logger = LoggerFactory
	.getLogger(BlogContentDaoImpl.class);
	
	public void insert(BlogContent blogContent) throws AppBusinessException {
		try {
			getSqlSession().insert(
					"cn.sumpu.app.guoguojie.persistence.BlogContentMapper.insert",
					blogContent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("Write database failure.", e);
			}
			throw new AppBusinessException("Write database failure.", e);
		}

	}

	@SuppressWarnings("unchecked")
	public List<BlogContent> getBlogContentList(Map<String, Object> map)
			throws AppBusinessException {

		try {
			return getSqlSession()
					.selectList(
							"cn.sumpu.app.guoguojie.persistence.BlogContentMapper.getBlogContentList",
							map);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("query database failure.", e);
			}
			throw new AppBusinessException("query database failure.", e);
		}
	}

	public void updateBlogContent(BlogContent blogContent) throws AppBusinessException {
		try {
			getSqlSession()
					.update("cn.sumpu.app.guoguojie.persistence.BlogContentMapper.updateBlogContent",
							blogContent);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("update BlogContent failure.", e);
			}
			throw new AppBusinessException("update BlogContent failure.", e);
		}

	}
	public void deleteBlogContentByBlog(String blog) throws AppBusinessException {
		try {
			getSqlSession()
					.delete("cn.sumpu.app.guoguojie.persistence.BlogContentMapper.deleteBlogContentByBlog",
							blog);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteLearning failure.", e);
			}
			throw new AppBusinessException("deleteLearning failure.", e);
		}
	}

	@Override
	public BlogContent getBlogContentById(String blog)
			throws AppBusinessException {
		try {
			return (BlogContent) getSqlSession()
					.selectOne("cn.sumpu.app.guoguojie.persistence.BlogContentMapper.getBlogContentById",
							blog);
		} catch (Exception e) {
			if (logger.isErrorEnabled()) {
				logger.error("deleteLearning failure.", e);
			}
			throw new AppBusinessException("deleteLearning failure.", e);
		}
	}
}
